package cqrtplm.listener;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import cqrtplm.service.PreventErrorCorrectionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;

import java.math.BigInteger;

@Slf4j
@CustomListener(category = "润通", description = "同步电子文件至关联问题报告附件")
public class CreateFileAndProblemItemListener implements ExecutionListener {
    @Override
    public void notify(DelegateExecution execution) {
        long start = System.currentTimeMillis();
        String workFlowContainerId = execution.getProcessInstanceBusinessKey();
        log.info("----------润通----同步电子文件至关联问题报告附件------开始------workFlowContainerId:{}----->>>",
                 workFlowContainerId);
        if (StringUtils.isBlank(workFlowContainerId)) {
            workFlowContainerId = execution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        String finalWorkFlowContainerId = workFlowContainerId;
        PreventErrorCorrectionService flowControlService = SpringUtil.getBean(PreventErrorCorrectionService.class);
        try {
            flowControlService.createFileAndProblemItemLink(new BigInteger(finalWorkFlowContainerId));
        } catch (Exception e) {
            log.info("同步电子文件至关联问题报告附件");
            log.error(ExceptionUtil.getMessage(e));
            log.error(ExceptionUtil.stacktraceToString(e, 100000));
        }
        log.info("----------润通----同步电子文件至关联问题报告附件,耗时：{}---->>>", System.currentTimeMillis() - start);
    }
}
